package org.example.climactic.controller;

import java.util.HashMap;
import java.util.Map;

public class BookSource {
    private String sourceId;
    private String sourceName;
    private String baseUrl;
    private Map<String, String> rules;
    
    public BookSource() {
    }
    
    public BookSource(String sourceId, String sourceName, String baseUrl, Map<String, String> rules) {
        this.sourceId = sourceId;
        this.sourceName = sourceName;
        this.baseUrl = baseUrl;
        this.rules = rules != null ? rules : new HashMap<String, String>();
    }
    
    // Getters and Setters
    public String getSourceId() {
        return sourceId;
    }
    
    public void setSourceId(String sourceId) {
        this.sourceId = sourceId;
    }
    
    public String getSourceName() {
        return sourceName;
    }
    
    public void setSourceName(String sourceName) {
        this.sourceName = sourceName;
    }
    
    public String getBaseUrl() {
        return baseUrl;
    }
    
    public void setBaseUrl(String baseUrl) {
        this.baseUrl = baseUrl;
    }
    
    public Map<String, String> getRules() {
        return rules;
    }
    
    public void setRules(Map<String, String> rules) {
        this.rules = rules;
    }
    
    public String getRule(String key, String defaultValue) {
        return rules.getOrDefault(key, defaultValue);
    }
    
    public Map<String, String> getRulesWithDefaults() {
        Map<String, String> defaultRules = new HashMap<String, String>();
        defaultRules.put("exploreUrl", "/");
        defaultRules.put("searchUrl", "/search");
        defaultRules.put("bookInfo.author", ".author");
        defaultRules.put("bookInfo.name", ".name");
        defaultRules.put("bookInfo.coverUrl", ".cover img@src");
        defaultRules.put("bookInfo.intro", ".intro");
        defaultRules.put("bookInfo.kind", ".kind");
        defaultRules.put("bookInfo.wordCount", ".word-count");
        defaultRules.put("bookInfo.lastChapter", ".last-chapter");
        defaultRules.put("bookInfo.tocUrl", ".toc-url");
        defaultRules.put("toc.chapterList", ".chapter-list");
        defaultRules.put("toc.chapterName", "text");
        defaultRules.put("toc.chapterUrl", "href");
        defaultRules.put("content.content", ".content");
        defaultRules.put("content.nextPage", ".next-page");
        defaultRules.put("toc.pagination", ".pagination");
        defaultRules.put("search.bookList", ".book-list");
        
        Map<String, String> combinedRules = new HashMap<String, String>(defaultRules);
        combinedRules.putAll(rules);
        return combinedRules;
    }
}